package com.blockchain.front.servlets;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.blockchain.framework.http.session.Session;
import com.blockchain.framework.http.session.SessionManager;
import com.blockchain.framework.http.session.authentication.AuthenticationException;
import com.blockchain.framework.http.session.authentication.PasswordAuthentication;
import com.blockchain.framework.http.session.authentication.VerifyCodeAuthentication;
import com.blockchain.framework.resource.PromptLevel;
import com.blockchain.framework.service.ServiceSession;
import com.blockchain.framework.service.exception.LogicalException;
import com.blockchain.framework.service.exception.ParameterException;
import com.blockchain.front.service.common.UserManage;
import com.blockchain.front.service.common.entity.UserInsert;

public class Register extends AbstractFrontServlet {
	private static final long serialVersionUID = 1L;

	@Override
	protected boolean mustAuthenticated() {
		return false;
	}

	@Override
	protected void processPost(final HttpServletRequest request,
			HttpServletResponse response, ServiceSession serviceSession)
			throws Throwable {
		Session session = serviceSession.getSession();
		if (session != null && session.isAuthenticated()) {
			sendRedirect(request,response,getController().getViewURI(request,  Index.class));
			return;
		}
		if (session == null) {
			session = getResourceProvider().getResource(SessionManager.class)
					.getSession(request, response, true);
		}
		VerifyCodeAuthentication authentication = new VerifyCodeAuthentication();
		String dxverifyCode = request.getParameter("dxverifyCode");
		authentication.setVerifyCodeType("REGISTER"+request.getParameter("phone"));
		authentication.setVerifyCode(dxverifyCode);
		try {
			session.authenticateVerifyCode(authentication);
		} catch (AuthenticationException e) {
			prompt(request, response, PromptLevel.ERROR, e.getMessage());
			forwardView(request, response, getClass());
			return;
		}
		UserManage userManage = serviceSession.getService(UserManage.class);
		final String accountName = request.getParameter("phone");
		final String password = request.getParameter("password");
		String newPassword = request.getParameter("newPassword");
		
		if (!password.equals(newPassword)) {
			getController().prompt(request, response, PromptLevel.ERROR,
					"两次密码输入不一致!");
			forwardView(request, response, getClass());
			return;
		}else if(userManage.isNickName(accountName)){
			prompt(request, response, PromptLevel.ERROR, "该用户已存在");
			forwardView(request, response, Register.class);
			return;
		}
		
		UserInsert user = new UserInsert() {


			@Override
			public String getPassword() {
				return password;
			}

			@Override
			public String getCode() {
				return request.getParameter("code");
			}


			@Override
			public String getPhone() {
				return request.getParameter("phone");
			}
			
		};
		final int userId = userManage.addUser(user);
		if (userId > 0) {
			serviceSession.commit();
			PasswordAuthentication passwordAuthentication = new PasswordAuthentication();
			passwordAuthentication.setAccountName(accountName);
			passwordAuthentication.setPassword(newPassword);
			passwordAuthentication.setVerifyCodeType("abc");
			passwordAuthentication.setVerifyCode(session
					.getVerifyCode(passwordAuthentication.getVerifyCodeType()));
			session.checkIn(request, response, passwordAuthentication);
			sendRedirect(request,response,getController().getViewURI(request,  RegisterTwo.class));
		}
	}
	@Override
	protected void onThrowable(HttpServletRequest request,
			HttpServletResponse response, Throwable throwable)
			throws ServletException, IOException {
		getResourceProvider().log(throwable.getMessage());
		if (throwable instanceof LogicalException
				|| throwable instanceof ParameterException) {
			prompt(request, response, PromptLevel.ERROR, throwable.getMessage());
			forwardView(request, response, getClass());
		} else if (throwable instanceof SQLException) {
			prompt(request, response, PromptLevel.ERROR, "系统繁忙，请稍后再试");
			forwardView(request, response, getClass());
		} else {
			super.onThrowable(request, response, throwable);
		}
	}
}
